package com.daisuxia.web.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daisuxia.web.dao.AppVersionDao;
import com.daisuxia.web.pojo.AppVersion;




@Service("appVersionService")
public class AppVersionService implements IAppVersionService {
	
	@Autowired
	private AppVersionDao appVersionDao;
	
	@Override
	public AppVersion queryObject(Integer id){
		return appVersionDao.queryObject(id);
	}
	
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return appVersionDao.queryTotal(map);
	}
	
	@Override
	public void save(AppVersion appVersion){
		appVersionDao.save(appVersion);
	}
	
	@Override
	public void update(AppVersion appVersion){
		appVersionDao.update(appVersion);
	}
	
	@Override
	public void delete(Integer id){
		appVersionDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		appVersionDao.deleteBatch(ids);
	}

	
	
	@Override
	public AppVersion getCurrentVersion(Map<String, String> param) {
		AppVersion result = new AppVersion();
		String OS = param.get("OS");
		String version = param.get("version");
		//查询客户端当前版本
		AppVersion custVersion = appVersionDao.getByOsandVersion(OS, version);
		//最新版本
		result = appVersionDao.getNewestVersion(OS);
		if(null==custVersion) {
			result.setIsForce(1);
		}else if(result.getIsForce()!=1){
			//如果发布的当前版本不是强制更新，查询客户端版本以后是否发布过强制更新的版本
			param.put("id", custVersion.getId()+"");
			List<AppVersion> versions =  appVersionDao.queryAfter(param);
			if(checkForce(versions)) {
				result.setIsForce(1);
			}
		}
		return result;
	}
	
	private boolean checkForce(List<AppVersion> appVersions) {
		boolean result = false;
		if(null!=appVersions) {
			for(AppVersion appVersion : appVersions) {
				if(appVersion.getIsForce()==1) {
					result = true;
				}
			}
		}
		return result;
	}
	
}
